package com.topisv.tms.utils.http;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.topisv.tms.entity.BackupTask;
import com.topisv.tms.entity.BackupTaskOrdeno;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.ShopSource;
import com.topisv.tms.service.BackupTaskOrdenoService;
import com.topisv.tms.service.BackupTaskService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.EdiOrderService;
import com.topisv.tms.service.ShopSourceService;
import com.topisv.tms.utils.vo.HtSendOutVo;

@Component
public class SynchroQueueMessage implements Runnable {
	private static final Logger log = LoggerFactory.getLogger(SynchroQueueMessage.class);

	public SynchroQueueMessage() {
		super();
	}

	@Autowired
	private EdiOrderService ediOrderService;
	@Autowired
	private BackupTaskOrdenoService backupTaskOrdenoService;
	@Autowired
	private ShopSourceService shopSourceService;
	@Autowired
	private CustomersService customersService;
	@Autowired
	private BackupTaskService backupTaskService;
	
	
	public void sendOutGoods(String backupTaskCode)throws Exception{
		List<BackupTaskOrdeno> backupTaskOrdenos=backupTaskOrdenoService.findByBackupTaskCode(backupTaskCode);
		BackupTask backupTask=backupTaskService.findByBackupTaskCode(backupTaskCode);
		for(BackupTaskOrdeno b:backupTaskOrdenos){
			log.info(b.getOrdeno()+"：开始发货");
			EdiOrder e=ediOrderService.findByOrdeno(b.getOrdeno());
			if("cancel".equals(e.getOrderStatus())){
				continue;
			}
			ShopSource shopSource=shopSourceService.findByShopCode(e.getShopCode());
			if(!"ht".equals(shopSource.getOrdenoSourceCode())){
				continue;
			}
			try {
				HtSendOutVo htSendOutVo=HttpPost.htSendOutGoods(e, shopSource);
				log.info(b.getOrdeno()+"：返回结果---"+htSendOutVo.toString());
				if("0".equals(htSendOutVo.getErrorCode())){
					b.setStatus("sendOut");//标记该订单已经发货
					backupTaskOrdenoService.save(b);
				}else if("1".equals(htSendOutVo.getErrorCode())){
					String message=htSendOutVo.getErrorMsg();
					if(message!=null){
						int index=message.indexOf("不能重复发货");
						if(index>0){//表示已经发货
							e.setOrderStatus("cancel");
							ediOrderService.save(e);
							try {
								Customers customers=customersService.findByCustomerCode(backupTask.getCustomerCode());
								String result = HttpPost.bmsCancelOrdeno(customers, b.getOrdeno());
								log.info("TMS返回的结果为:" + result);
							} catch (Exception e1) {
								log.error("TMS取消接口异常", e1);

							}
						}
					}
					
				}
			} catch (Exception e2) {
				log.error("", e2);
			}
		}
	}

	
	@Override
	public void run() {
		while (true) {
			String message = null;
			try {
				message = SynchroShippedBatchQueue.takeSynchroQueue();
				this.sendOutGoods(message);
			} catch (Exception e) {
				log.error("error",e);
				
			}
		}
	}
	
	
}
